package com.iiiss.template.ssh.impl.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.iiiss.template.ssh.core.beans.ISnsUser;
import com.iiiss.template.ssh.core.daos.ISnsUserDao;
import com.iiiss.template.ssh.core.services.ISnsUserService;

/*
 * TemplateSSH - com.iiiss.template.ssh.impl.services - SnsUserService.java
 * 
 * Copyright (C) 2011  Mingye Cheng <Cheng, Mingye at GMAIL>
 * 
 * SnsUserService.java is part of <iiiStudio Template Framework> project.
 * This project is hosted at <http://iiiss-template-ssh.googlecode.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Revision history:
 * [2011-8-9] Mingye Cheng - Initial version established.
 * [2011-8-27] Mingye Cheng - Using generic types for Lists,
 *                            Using Exception mechanism for non-existent users,
 *                            Function 'searchFriends()' added.
 * [2011-9-2] Mingye Cheng - Interfaces of 'IUserDao' changed.
 */
@Transactional
public class SnsUserService extends UserService implements ISnsUserService {
	@Autowired
	private ISnsUserDao snsUserDao;

	public void setSnsUserDao(ISnsUserDao snsUserDao) {
		this.snsUserDao = snsUserDao;
	}

	public boolean isFriend(String current, String target)
			throws NullPointerException {
		ISnsUser currentUser = snsUserDao.getUserByUsername(current);
		ISnsUser targetUser = snsUserDao.getUserByUsername(target);
		return snsUserDao.isFriend(currentUser, targetUser);
	}

	public boolean addFriend(String current, String target)
			throws NullPointerException {
		if (current.equals(target))
			return false;

		ISnsUser currentUser = snsUserDao.getUserByUsername(current);
		ISnsUser targetUser = snsUserDao.getUserByUsername(target);

		if (snsUserDao.isFriend(currentUser, targetUser))
			return false;

		snsUserDao.addFriend(currentUser, targetUser);
		return true;
	}

	public int countFriends(String current, String target)
			throws NullPointerException {
		if (target == null)
			target = current;

		ISnsUser targetUser = snsUserDao.getUserByUsername(target);
		return snsUserDao.countFriends(targetUser);
	}

	public int countFollowers(String current, String target)
			throws NullPointerException {
		if (target == null)
			target = current;

		ISnsUser targetUser = snsUserDao.getUserByUsername(target);
		return snsUserDao.countFollowers(targetUser);
	}

	@SuppressWarnings("unchecked")
	public <T extends ISnsUser> List<T> getFriends(String current, String target)
			throws NullPointerException {
		if (target == null)
			target = current;

		ISnsUser targetUser = snsUserDao.getUserByUsername(target);
		List<T> dbFriends = (List<T>) snsUserDao.getFriends(targetUser);

		List<T> friends = new ArrayList<T>();
		friends.addAll(dbFriends);
		return friends;
	}

	@SuppressWarnings("unchecked")
	public <T extends ISnsUser> List<T> getFollowers(String current,
			String target) throws NullPointerException {
		if (target == null)
			target = current;

		ISnsUser targetUser = snsUserDao.getUserByUsername(target);
		List<T> dbFollowers = (List<T>) snsUserDao.getFollowers(targetUser);

		List<T> followers = new ArrayList<T>();
		followers.addAll(dbFollowers);
		return followers;
	}

	public boolean removeFriend(String current, String target)
			throws NullPointerException {
		ISnsUser currentUser = snsUserDao.getUserByUsername(current);
		ISnsUser targetUser = snsUserDao.getUserByUsername(target);

		if (!snsUserDao.isFriend(currentUser, targetUser))
			return false;

		snsUserDao.removeFriend(currentUser, targetUser);
		return true;
	}

	public <T extends ISnsUser> List<T> searchFriends(String current,
			String target, String keyword) throws NullPointerException {
		List<T> friends = this.getFriends(current, target);
		List<T> filteredFriends = new ArrayList<T>();
		for (T friend : friends)
			if (friend.getUsername().contains(keyword))
				filteredFriends.add(friend);
		return filteredFriends;
	}
}
